package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.StartBundleKey;
import com.zlzxm.shoesmarket.view.activity.manage.bundle.BundleGoodsInfo;
import com.zlzxm.shoesmarket.view.adapter.ADGoodsSizeOperate;
import com.zlzxm.shoesmarket.view.adapter.ADGoodsSizeTag;
import com.zlzxm.shoesmarket.view.widget.GoodsInfoView;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.viewmodel.VMSaveEdit;

/**
 * CREATE BY zlzxm ON 4/20/21
 * <br>
 *    库存编辑
 */
public class ACSaveEdit extends  BaseActivity implements OnItemClickListener, OnItemChildClickListener {


    public static void start(Context context, BundleGoodsInfo bundleGoodsInfo) {
        Intent starter = new Intent(context, ACSaveEdit.class);
        starter.putExtra(StartBundleKey.KEY_GOODS_INFO,bundleGoodsInfo);
        context.startActivity(starter);
    }

    VMSaveEdit vmSaveEdit;

    GoodsInfoView goodsInfoView;

    private RecyclerView rvSize,rvSizeDetail;

    private ADGoodsSizeTag adGoodsSizeTag = new ADGoodsSizeTag();

    private ADGoodsSizeOperate adGoodsSizeOperate = new ADGoodsSizeOperate();


    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_save_edit);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("库存编辑");
        simpleHead.setOnItemclicklistener(this::finish);
        goodsInfoView  = ZViewHelp.findById(this,R.id.goodsInfoView);

        rvSize = ZViewHelp.findById(this,R.id.rvSize);
        rvSizeDetail = ZViewHelp.findById(this,R.id.rvSizeDetail);

        rvSize.setLayoutManager(new FlexboxLayoutManager(this));

        rvSizeDetail.setLayoutManager(new LinearLayoutManager(this));

        adGoodsSizeTag.setOnItemClickListener(this);

        adGoodsSizeOperate.setOnItemChildClickListener(this);
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmSaveEdit = new ViewModelProvider(this).get(VMSaveEdit.class);

        vmSaveEdit.getLiveGoodsInfo().observe(this,it ->{
//
//            updateGoodsInfo(it.getBrand(),it.getCode(),
//                    it.getName(),it.getFace());

//            adGoodsSizeTag.setList(it.getList());


        });

        vmSaveEdit.getLiveSaveSize().observe(this,it -> adGoodsSizeOperate.setList(it));
    }

    @Override
    protected void initData() {
        super.initData();
        updateInfo();
        rvSizeDetail.setAdapter(adGoodsSizeOperate);
        rvSize.setAdapter(adGoodsSizeTag);
        vmSaveEdit.requestSize();
        vmSaveEdit.requestSizeInfo();
    }



    private void updateInfo(){

        BundleGoodsInfo bundleGoodsInfo = (BundleGoodsInfo) getIntent().getSerializableExtra(StartBundleKey.KEY_GOODS_INFO);

        goodsInfoView.updateBrand(bundleGoodsInfo.getBrand());
        goodsInfoView.updateCode(bundleGoodsInfo.getCode());
        goodsInfoView.updateFace(bundleGoodsInfo.getFace());
        goodsInfoView.updateName(bundleGoodsInfo.getName());
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        adGoodsSizeTag.setSelectPosition(position);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {

        if(view.getId() == R.id.ivSub){

            if(adGoodsSizeOperate.getData().get(position).sub()){

                adGoodsSizeOperate.notifyDataSetChanged();
            }

        }else if(view.getId() == R.id.ivAdd){

            adGoodsSizeOperate.getData().get(position).add();
            adGoodsSizeOperate.notifyDataSetChanged();
        }


    }
}
